package com.cloudptt.wp.ui;

import java.io.File;
import java.io.FileInputStream;

import org.xmlpull.v1.XmlPullParser;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class LoginConfig {
	private static String TAG = "LoginConfig";
	private static final String LoginConfigPath = Environment.getExternalStorageDirectory() + "/loginconfig/";
	private static final String Name = "loginconfig.xml";
	private static final String Key_TAG = "loginconfig";
	private static final String Key_ID = "infomation";
	private static final String Key_Name = "username";
	private static final String Key_Pwd = "password";
	private static final String Key_ServerIp = "serverip";
	private static final String Key_Port = "port";
	private static final String Key_Dev = "watch";

	private boolean isLoginConfigFolderExist() {
		File path = new File(LoginConfigPath);
		Log.i(TAG, "path---" + path);
		if (path.exists()) {
			return true;
		}
		return false;
	}

	public LoginConfigData readxml() {
		LoginConfigData configData = null;
		
		if (!isLoginConfigFolderExist()) {
			return null;
		}
		try {
			
			File path = new File(LoginConfigPath, Name);
			FileInputStream fis = new FileInputStream(path);

			// 获得pull解析器对象
			XmlPullParser parser = Xml.newPullParser();
			// 指定解析的文件和编码格式
			parser.setInput(fis, "utf-8");

			int eventType = parser.getEventType(); // 获得事件类型
			
			String id = null;  
			String username = null;
			String password = null;
			String serverip = null;
			String port = null;
			String device = null;
			  while (eventType != XmlPullParser.END_DOCUMENT) {  
	                String tagName = parser.getName(); // 获得当前节点的名称  
	                switch (eventType) {  
	                case XmlPullParser.START_TAG: // 当前等于开始节点 <person>  
	                    if (Key_TAG.equals(tagName)) { // <persons>  
	                    } else if (Key_ID.equals(tagName)) { // <person id="1">  
	                        id = parser.getAttributeValue(null, "id");  
	                    } else if (Key_Name.equals(tagName)) { // <name>  
	                    	username = parser.nextText();  
	                    }else if (Key_Pwd.equals(tagName)) { // <age>  
	                    	password = parser.nextText();  
	                    } else if (Key_ServerIp.equals(tagName)) { // <age>  
	                    	serverip = parser.nextText();  
	                    }else if (Key_Port.equals(tagName)) { // <age>  
	                    	port = parser.nextText();  
	                    }else if (Key_Dev.equals(tagName)) {//device watch
	                    	device = parser.nextText();
	                    	Log.i(TAG, "device---"+ device);
	                    }    
	                    break;  
	                case XmlPullParser.END_TAG: // </persons>  
	                    if (Key_ID.equals(tagName)) {  
	                       Log.i(TAG, "id---" + id+",username---" + username +",password---" + password+",serverip---" + serverip+",port---" + port);  
	                    }  
	                    break;  
	                default:  
	                    break;  
	                }  
	                eventType = parser.next(); // 获得下一个事件类型  
	            }
				
			  fis.close();
			  configData = new LoginConfigData(username, password, serverip, port,device);
			  Log.i(TAG, "### username---" + username +",password---" + password+",serverip---" + serverip+",port---" + port);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return configData;
	}

}
